﻿using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TFSTools.TFSLibrary
{
    public sealed class TFSRevision : TFSServerEntity
    {
        private Revision serverRevision;
        private TFSFieldInfoCollection fields = null;

        internal TFSRevision(ITFSConnection connection, Revision revision)
            : base(connection)
        {
            this.serverRevision = revision;
        }

        internal Revision ServerEntity { get { return this.serverRevision; } }

        public DateTime ChangedDate { get { return (DateTime)this.serverRevision.Fields["Changed Date"].Value; } }

        public string ChangedBy { get { return this.serverRevision.Fields["Changed By"].Value.ToString(); } }

        public TFSFieldInfoCollection Fields
        {
            get
            {
                if (this.fields == null)
                {
                    this.fields = TFSFieldInfoCollection.Create(this.Connection, this.serverRevision.Fields);
                }

                return this.fields;
            }
        }

        public override string ToString() { return this.serverRevision.ToString(); }
    }

    public sealed class TFSRevisionCollection : TFSServerEntityCollection<TFSRevision>
    {
        public TFSRevisionCollection(ITFSConnection connection)
            : base(connection)
        {
        }

        public TFSRevisionCollection(ITFSConnection connection, IList<TFSRevision> list)
            : base(connection, list)
        {
        }

        public TFSRevisionCollection(ITFSConnection connection, int capacity)
            : base(connection, capacity)
        {
        }

        internal static TFSRevisionCollection Create(ITFSConnection connection, RevisionCollection collection)
        {
            var list = new List<TFSRevision>(collection.Count);

            foreach (Revision revision in collection)
            {
                list.Add(new TFSRevision(connection, revision));
            }

            return new TFSRevisionCollection(connection, list);
        }
    }
}